package com.sinocare.project.dict.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.project.common.domin.*;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.common.vo.DrugStorePatientVo;
import com.sinocare.project.dict.service.*;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

//@CrossOrig
@CrossOrigin
@RestController
@RequestMapping("/dict")
@Api(tags = "dfghj")
public class DictController {
    @Resource
    private Service service;
    @Resource
    private UserLoginService userLoginService;
    @Resource
    private SccMedicalResultService resultService;
    @Resource
    private BaseService baseService;
    @Resource
    private SccReportListService listService;
    @RequestMapping(value = "pageSelect",method = RequestMethod.GET)
    public Result userId(int page,int limit,String searchParams)throws Exception{//分页查询
        /**
         * 分页查询加搜索
         */
        ObjectMapper objectMapper = new ObjectMapper();
        DrugStorePatientVo patientVo = null;
        if (searchParams != null) {
            patientVo = objectMapper.readValue(searchParams, DrugStorePatientVo.class);
        }
        Page page1= PageHelper.startPage(page,limit);
        List<Map<String, Object>> maps = service.queryAll(patientVo);
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),maps);
    }


    /**
     * 检测者列表修改按钮
     * @param sccPatient
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.GET)
    public Result updateDetector(SccPatient sccPatient){
        QueryWrapper<SccPatient> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("patient_id",sccPatient.getPatientId());
        boolean update = service.update(sccPatient, queryWrapper);
        return Result.ok(ResultCode.SUCCESS,update);

    }

    /**
     * 检测者列表删除按钮
     * @param sccPatient
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public Result deleteDetector(SccPatient sccPatient){
        QueryWrapper<SccPatient> queryWrapper = new QueryWrapper<>();
        sccPatient.setStatus(0);
        queryWrapper.eq("patient_id",sccPatient.getPatientId());
        service.update(sccPatient,queryWrapper);
        return Result.ok(ResultCode.SUCCESS,0);

    }

    /**
     * 添加操作，新增检测者
     * @param sccPatient1
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.GET)
    public Result insertDetector(SccPatient sccPatient1){
        QueryWrapper<SccPatient> queryWrapper = new QueryWrapper<>();
        SccUserLogin sccUserLogin=new SccUserLogin();
        sccUserLogin.setName(sccPatient1.getName());
        System.out.println(sccPatient1.getName());
        sccUserLogin.setPassword("888888");
        sccUserLogin.setId(0);
        sccUserLogin.setPhone(sccPatient1.getPhone());
        boolean save = userLoginService.save(sccUserLogin);
        double d = Math.random();
        int i = (int)(d*10000000);
        sccPatient1.setPatientCode("No"+i);
        sccPatient1.setUserId(1);
        double dou = Math.random();
        int c = (int)(dou*100000);
        sccPatient1.setCustomerCode("ENF"+c);
        boolean b = service.save(sccPatient1);
        queryWrapper.eq("patient_id",sccPatient1.getPatientId());
        return Result.ok(ResultCode.SUCCESS,sccUserLogin);
    }

    /**
     * 添加检测项目
     * @param sccMedicalResult1
     * @return
     */
    @RequestMapping(value = "insertTestItems",method = RequestMethod.GET)
    public Result insertDetectorProject(SccMedicalResult sccMedicalResult1){
        QueryWrapper<SccMedicalResult> queryWrapper = new QueryWrapper<>();
        SccReportList sccReportList=new SccReportList();
        sccReportList.setId(0);
        baseService.insertMapper(sccMedicalResult1);
        sccReportList.setResultId(sccMedicalResult1.getResultId());
        System.out.println(sccMedicalResult1.getResultId());
        if(sccMedicalResult1.getGluResult()!=null){
            sccReportList.setMedicalCode("GLU");
            sccReportList.setMedicalName("血糖值");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("3.9-6.1");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getGluResult());
            if(sccMedicalResult1.getGluResult()>=3.9 && sccMedicalResult1.getGluResult()>=6.1){
                sccReportList.setMedicalStatus(0);
            }else {
                sccReportList.setMedicalStatus(1);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }else if(sccMedicalResult1.getKetResult()!=null){
            sccReportList.setMedicalCode("BK");
            sccReportList.setMedicalName("血酮");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("0.03-0.5");

            sccReportList.setMedicalResult(""+sccMedicalResult1.getKetResult());
            if(sccMedicalResult1.getKetResult()>=0.03 && sccMedicalResult1.getKetResult()>=0.5){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }else if (sccMedicalResult1.getUaResult()!=null){
            sccReportList.setMedicalCode("UA");
            sccReportList.setMedicalName("血尿酸");
            sccReportList.setMedicalUnits("umol/L");
            sccReportList.setMedicalMethod("电"+"化学");
            sccReportList.setMedicalReference("181-416");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getUaResult());
            if(sccMedicalResult1.getUaResult()>181 && sccMedicalResult1.getUaResult()>416){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }else if (sccMedicalResult1.getHbaicResult()!=null){
            sccReportList.setMedicalCode("HbA1c");
            sccReportList.setMedicalName("糖化血红蛋白");
            sccReportList.setMedicalUnits("%");
            sccReportList.setMedicalMethod("亲和色谱法");
            sccReportList.setMedicalReference("4-6%");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getHbaicResult());
            if(sccMedicalResult1.getHbaicResult()>=181 && sccMedicalResult1.getHbaicResult()>=416){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getCholResult()!=null){
            sccReportList.setMedicalCode("TC");
            sccReportList.setMedicalName("总胆固醇");
            sccReportList.setMedicalUnits("umol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("1.04-1.54");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getCholResult());
            if(sccMedicalResult1.getCholResult()>=1.04 && sccMedicalResult1.getCholResult()>=1.54){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getTgResult()!=null){
            sccReportList.setMedicalCode("TG");
            sccReportList.setMedicalName("甘油三脂");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("0.6-1.7");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getTgResult());
            if(sccMedicalResult1.getTgResult()>0.6 && sccMedicalResult1.getTgResult()>1.7){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getHdlCResult()!=null){
            sccReportList.setMedicalCode("LDL-C");
            sccReportList.setMedicalName("高密度脂蛋白胆固醇");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("3.37");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getHdlCResult());
            if(sccMedicalResult1.getHdlCResult()>=1.04 && sccMedicalResult1.getHdlCResult()>=1.54){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getLdlCResult()!=null){
            sccReportList.setMedicalCode("LDL-C");
            sccReportList.setMedicalName("底密度脂蛋白胆固醇");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("3.37");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getLdlCResult());
            if(sccMedicalResult1.getLdlCResult()>=3.37 && sccMedicalResult1.getLdlCResult()>=3.4){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getTcHdlCResult()!=null){
            sccReportList.setMedicalCode("TC-H");
            sccReportList.setMedicalName("总胆/高密比值");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("3.37");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getTcHdlCResult());
            if(sccMedicalResult1.getTcHdlCResult()>=3.37 && sccMedicalResult1.getTcHdlCResult()>=3.4){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getSbpResult()!=null){
            sccReportList.setMedicalCode("SBP");
            sccReportList.setMedicalName("收缩压");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("低于140mmHg");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getSbpResult());
            if(sccMedicalResult1.getSbpResult()<=140){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }
            if (sccMedicalResult1.getDbpResult()!=null){
            sccReportList.setMedicalCode("DBP");
            sccReportList.setMedicalName("舒张压");
            sccReportList.setMedicalUnits("mmol/L");
            sccReportList.setMedicalMethod("电化学");
            sccReportList.setMedicalReference("低于90mmHg");
            sccReportList.setMedicalResult(""+sccMedicalResult1.getDbpResult());
            if(sccMedicalResult1.getDbpResult()<=90){
                sccReportList.setMedicalStatus(1);
            }else {
                sccReportList.setMedicalStatus(0);
            }
            sccReportList.setMedicalSuggest(sccMedicalResult1.getRemark());
        }

//        boolean save = resultService.save(sccMedicalResult1);

        boolean save1 = listService.save(sccReportList);
        queryWrapper.eq("patient_id",sccMedicalResult1.getPatientId());

        return Result.ok(ResultCode.SUCCESS,sccReportList);
    }

    /**
     * 下拉框动态获取值,检测者列表
     * @return
     */
    @RequestMapping(value = "Base",method = RequestMethod.GET)
    public Result selectFenDian(){
        List<sccDrugstore> sccDrugstores = baseService.findBase();
        return Result.ok(ResultCode.SUCCESS,sccDrugstores);
    }

    @RequestMapping(value = "{patientId}",method = RequestMethod.GET)
    public Result selectSccMe2(@PathVariable("patientId") Integer patientId) throws JsonProcessingException {
        List<Map<String,Object>> sccMedicalResults = baseService.selectSccMe(patientId);
        return Result.ok(ResultCode.SUCCESS,sccMedicalResults);
    }
}
